package controller;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import view.gamePanel;

import model.gameModelI;

/*****************************************
 * userInputController
 * 
 * delegates events to frogState
 * 
 * @author Stephen Gregory, Jun Wang
 *
 */

public class userInputController {
	static final frogState onLogState = new onLog();
	static final frogState crossedRiverState = new crossedRiver();
	static final frogState jumpingState = new jumping();
	static final frogState onBankState = new onBank();
	static final frogState gameOverState = new gameOver();
	private frogState currentState;
	gameModelI model;
	gamePanel view;
	screenController screen;
	 
	public userInputController(gameModelI m, gamePanel v, screenController s) {
		this.currentState = onBankState;	
		this.model = m;
		this.view = v;		
		this.screen = s;
	}
	
	public void setView(gamePanel v) {
		this.view = v;
	}
	
	public void processInput(char keyinput) {
		currentState.handleInput(this, keyinput);
		view.repaint();
	}
	 
	protected void setState(frogState state) {
		this.currentState = state;
	}
	
	public void reset() {
		this.currentState = onBankState;
	}

	public void frogHitEdge() {
		currentState.frogHitEdge(this);
		view.repaint();
		
		if ( currentState == gameOverState) {
			String name = null;
			if (model.isHighScore()) {
				name = JOptionPane.showInputDialog(null, "Game over! You reached a new high score! Please enter your name: ");
				model.setHighScore(name); //a way to cancel
			}
			else
				JOptionPane.showMessageDialog(null, "Game Over! Your score was " + model.getTotalScore());
			
			screen.endGame();
		}
	}
	
}
